package controller

import (
	"github.com/gin-gonic/gin"
	"shop-bean/dto"
	"shop-bean/model"
	"shop-common/library/serializer"
	"shop-common/utils"
	"shop-common/utils/contextArgs"
	"shop-service/business"
)

type NoticeController struct {
}

// @Tags 公告管理接口
// @description: 置顶公告列表信息
// @Summary 获取所有置顶公告列表信息
// @function: GetTopNoticeList
// @Accept  json
// @Produce  json
// @Success 200 {object} string "{"code":1," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /shop/notice/topNoticeList [get]
func (c *NoticeController) GetTopNoticeList(ctx *gin.Context) {
	notices, err := business.ServiceImpl().NoticeService.FindTopNotice(ctx)
	if err != nil {
		serializer.Fail(ctx, serializer.PublicQueryErr)
		return
	}

	var noticesDto []*dto.NoticeDto
	utils.StructMap(notices, &noticesDto)
	serializer.SuccessData(ctx, "", noticesDto)
}

// @Tags 公告管理接口
// @description: 公告详情
// @Summary 获取公告id公告详情
// @function: GetNoticeById
// @Accept  json
// @Produce  json
// @Param id path string true "主键ID"
// @Success 200 {object} string "{"code":1," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /shop/notice/info/{id} [get]
func (c *NoticeController) GetNoticeById(ctx *gin.Context) {
	id, err := contextArgs.GetUriIntPath(ctx, "id")
	if err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	// todo @Cacheable  后期需要添加缓存。
	notices, err := business.ServiceImpl().NoticeService.FindById(ctx, uint(id))
	if err != nil {
		serializer.Fail(ctx, serializer.PublicQueryErr)
		return
	}

	var noticesDto *model.Notice
	utils.StructMap(notices, &noticesDto)
	serializer.SuccessData(ctx, "", noticesDto)
}
